package com.qjdchina.sku.biz.impl;

import java.util.*;

import com.qjdchina.sku.dao.mapper.CategoryBrandAssociateMapper;
import com.qjdchina.sku.dao.mapper.SkuItemMapper;
import com.qjdchina.sku.enums.ItemValueType;
import com.qjdchina.sku.model.CategoryBrandAssociate;
import com.qjdchina.sku.model.SkuItem;
import com.qjdchina.sku.model.vo.CategoryBrandAssoVO;
import com.qjdchina.sku.model.vo.CategoryBrandRegionVO;
import com.qjdchina.sku.util.CategoryBrandBusiUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qjdchina.commons.enums.IsDeleted;
import com.qjdchina.commons.model.PageModel;
import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.BrandManager;
import com.qjdchina.sku.converter.BrandConverter;
import com.qjdchina.sku.dao.mapper.BrandMapper;
import com.qjdchina.sku.model.Brand;
import com.qjdchina.sku.model.query.BrandQueryVO;
import com.qjdchina.sku.model.vo.BrandVO;
import com.qjdchina.sku.service.enums.SkuResultCode;

@Service
public class BrandManagerImpl implements BrandManager {
	@Autowired
	private BrandMapper brandMapper;

	@Autowired
	private SkuItemMapper skuItemMapper;

	@Autowired
	private CategoryBrandAssociateMapper categoryBrandAssociateMapper;

	@Autowired
	private CategoryBrandBusiUtil categoryBrandBusiUtil;

	@Override
	public Result<List<BrandVO>> getBrandByCategoryId(Integer categoryId, List<Integer> skuRoleIds,
													  Integer skuCompanyId) {
		if (categoryId == null) {
			return new Result<List<BrandVO>>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		List<BrandVO> brandVOs = brandMapper.selectByCategoryId(categoryId, skuRoleIds);
		if (brandVOs == null) {
			return new Result<List<BrandVO>>(SkuResultCode.DATA_NOT_EXIST);
		}
		List<Integer> categoryBrandIdList = new LinkedList<>();
		for (BrandVO brandVO : brandVOs) {
			categoryBrandIdList.add(brandVO.getCategoryBrandId());
		}
		//查询获取品类品牌属性值映射关系
		Map<Integer, List<CategoryBrandAssoVO>> categoryBrandVOMap = getCategoryBrandVOMap(categoryId, categoryBrandIdList);
		//查询获取品类品牌运费调差值映射关系
		Map<Integer, List<CategoryBrandRegionVO>> categoryBrandRegionVOMap = new LinkedHashMap<>();
		for (BrandVO brandVO : brandVOs) {
			List<CategoryBrandRegionVO> categoryBrandRegionVOList = categoryBrandBusiUtil.getCategoryBrandRegionVOList(brandVO
					.getCategoryBrandId());
			categoryBrandRegionVOMap.put(brandVO.getCategoryBrandId(), categoryBrandRegionVOList);
		}

		if (skuCompanyId == null) {
			return new Result<List<BrandVO>>(brandVOs);
		}
		List<Brand> brands = brandMapper.selectBrandBySkuCompanyId(skuCompanyId);
		List<Integer> brandIdList = new ArrayList<>();
		for (Brand brand : brands) {
			brandIdList.add(brand.getId());
		}
		List<BrandVO> brandVOList = new ArrayList<>();
		for (BrandVO brandVO : brandVOs) {
			Integer id = brandVO.getId();
			if (brandIdList.contains(id)) {
				if (categoryBrandVOMap != null && categoryBrandVOMap.size() > 0) {
					List<CategoryBrandAssoVO> categoryBrandAssoVOList = categoryBrandVOMap.get(brandVO.getCategoryBrandId());
					if (categoryBrandAssoVOList != null && categoryBrandAssoVOList.size() > 0) {
						brandVO.setCategoryBrandAssoVOList(categoryBrandAssoVOList);
					}
				}
				if (categoryBrandRegionVOMap.size() > 0) {
					List<CategoryBrandRegionVO> categoryBrandRegionVOList = categoryBrandRegionVOMap.get(brandVO
							.getCategoryBrandId());
					if (categoryBrandRegionVOList != null && categoryBrandRegionVOList.size() > 0) {
						brandVO.setCategoryBrandRegionVOList(categoryBrandRegionVOList);
					}
				}
				brandVOList.add(brandVO);
			}
		}

		return new Result<List<BrandVO>>(brandVOList);
	}

	/**
	 * 根据ID获取品牌 shenpingfeng 2017年1月5日
	 * 
	 * @param brandId
	 * @return
	 */
	@Override
	public Result<BrandVO> getBrandById(Integer brandId) {
		if (brandId == null) {
			return new Result<BrandVO>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		Brand brand = brandMapper.selectByPrimaryKey(brandId);
		if (brand == null) {
			return new Result<BrandVO>(SkuResultCode.DATA_NOT_EXIST);
		}
		BrandVO brandVO = BrandConverter.toBrandVO(brand);
		return new Result<BrandVO>(brandVO);
	}

	/**
	 * 更新品牌 shenpingfeng 2017年1月10日
	 * 
	 * @param brandVO
	 * @return
	 */
	@Override
	public Result<String> update(BrandVO brandVO) {
		if (brandVO == null) {
			return new Result<String>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		// 校验品牌是否存在
		Brand cBrand = brandMapper.selectByPrimaryKey(brandVO.getId());
		if (cBrand == null) {
			return new Result<String>(SkuResultCode.DATA_NOT_EXIST);
		}
		// 根据label获取品牌是否存在
		Brand b = brandMapper.selectByLabel(brandVO.getLabel());
		if (b != null && !b.getId().equals(brandVO.getId())) {
			return new Result<>(SkuResultCode.BRAND_NAME_REPEAT);
		}
		Brand brand = BrandConverter.toBrand(brandVO);
		brand.setGmtModified(new Date());
		int row = brandMapper.updateByPrimaryKeySelective(brand);
		if (row == 0) {
			return new Result<String>(SkuResultCode.UPDATE_FAIL);
		}
		return new Result<String>(brandVO.getLabel());
	}

	/**
	 * 创建品牌 shenpingfeng 2017年1月10日
	 * 
	 * @param brandVO
	 * @return
	 */
	@Override
	public Result<String> save(BrandVO brandVO) {
		if (brandVO == null) {
			return new Result<String>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		Brand brand = BrandConverter.toBrand(brandVO);

		brand.setGmtCreated(new Date());
		brand.setGmtModified(new Date());
		brand.setIsDeleted(IsDeleted.NO.getValue());
		String label = brand.getLabel();
		// 校验label是否唯一
		Brand cBrand = brandMapper.selectByLabel(label);
		if (cBrand != null) {
			return new Result<String>(SkuResultCode.BRAND_NAME_REPEAT);
		}
		int row = brandMapper.insertSelective(brand);
		if (row == 0) {
			return new Result<String>(SkuResultCode.ADD_FAIL);
		}
		return new Result<String>(brandVO.getLabel());
	}

	/**
	 * 
	 * 根据名称获取 shenpingfeng 2017年1月10日
	 * 
	 * @param label
	 * @return
	 */
	@Override
	public Result<BrandVO> getBrandByLabel(String label) {
		if (StringUtils.isBlank(label)) {
			return new Result<BrandVO>(SkuResultCode.PARAMETER_NOT_EMPTY);
		}
		Brand brand = brandMapper.selectByLabel(label);
		if (brand == null) {
			return new Result<BrandVO>(SkuResultCode.DATA_NOT_EXIST);
		}
		BrandVO brandVO = BrandConverter.toBrandVO(brand);
		return new Result<BrandVO>(brandVO);
	}

	/**
	 * 根据skuCompanyId获取品牌 shenpingfeng 2017年1月13日
	 * 
	 * @param skuCompanyId
	 * @return
	 */
	@Override
	public Result<List<BrandVO>> getBrandBySkuCompanyId(Integer skuCompanyId) {
		List<Brand> brands = brandMapper.selectBrandBySkuCompanyId(skuCompanyId);
		List<BrandVO> brandVOs = BrandConverter.toBrandVOs(brands);
		return new Result<List<BrandVO>>(brandVOs);
	}

	/**
	 * 分页查询品牌列表 shenpingfeng 2017年2月13日
	 * 
	 * @param brandQueryVO
	 * @param pageModel
	 * @return
	 */
	@Override
	public Result<PageModel<BrandVO>> queryPagedBrandList(BrandQueryVO brandQueryVO, PageModel<BrandVO> pageModel) {
		Map<String, Object> params = convertBrandQueryVOToMap(brandQueryVO);
		int pagedBrandTotal = brandMapper.getPagedBrandTotal(params);
		List<Brand> brands = brandMapper.queryPagedBrandList(params, pageModel);
		List<BrandVO> brandVOs = BrandConverter.toBrandVOs(brands);
		pageModel.setPagedRecords(brandVOs);
		pageModel.setTotalCount(pagedBrandTotal);
		return new Result<PageModel<BrandVO>>(pageModel);

	}

	private Map<String, Object> convertBrandQueryVOToMap(BrandQueryVO queryVO) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", queryVO.getId());
		params.put("label", queryVO.getLabel());
		return params;

	}

	/**
	 * 获取所有的品牌 shenpingfeng 2017年2月15日
	 * 
	 * @return
	 */
	@Override
	public Result<List<BrandVO>> getAllBrand() {
		List<Brand> brands = brandMapper.selectAllBrand();
		if (brands == null || brands.size() == 0) {
			return new Result<List<BrandVO>>(SkuResultCode.DATA_NOT_EXIST);
		}
		List<BrandVO> brandVOs = BrandConverter.toBrandVOs(brands);
		return new Result<List<BrandVO>>(brandVOs);
	}

	/**
	 * 根据品类获取品牌 shenpingfeng 2017年2月21日
	 * 
	 * @param categoryId
	 * @return
	 */
	@Override
	public Result<List<BrandVO>> getBrandByCategory(Integer categoryId) {
		List<BrandVO> brandVOs = brandMapper.selectByCategoryId(categoryId, null);
		return new Result<List<BrandVO>>(brandVOs);

	}

	/**
	 * 品类品牌属性值映射关系
	 *
	 * @param categoryId 品类id
	 * @param categoryBrandIdList 品类品牌关联表ids
	 * @return Map<Integer, List<CategoryBrandAssoVO>>
	 */
	private Map<Integer, List<CategoryBrandAssoVO>> getCategoryBrandVOMap(Integer categoryId, List<Integer>
			categoryBrandIdList) {
		//品类品牌属性值映射关系
		Map<Integer, List<CategoryBrandAssoVO>> categoryBrandVOMap = new LinkedHashMap<>();
		List<SkuItem> skuItemList = skuItemMapper.selectByCategoryIdAndValueType(categoryId, ItemValueType.HIDDEN.getValue());
		List<Integer> skuItemIdList = new LinkedList<>();
		if (skuItemList != null && skuItemList.size() > 0 && categoryBrandIdList.size() > 0) {
			//表头名称映射关系
			Map<Integer, String> skuItemNameMap = new LinkedHashMap<>();
			for (SkuItem skuItem : skuItemList) {
				skuItemIdList.add(skuItem.getId());
				skuItemNameMap.put(skuItem.getId(), skuItem.getSkuName());
			}

			//skuCategoryBrandId和CategoryBrandAssociate映射关系
			Map<Integer, List<CategoryBrandAssociate>> categoryBrandAssociateMap = new LinkedHashMap<>();
			List<CategoryBrandAssociate> categoryBrandAssociateList = categoryBrandAssociateMapper
					.selectByCategoryBrandIdsAndItemIds(categoryBrandIdList, skuItemIdList);
			if (categoryBrandAssociateList != null && categoryBrandAssociateList.size() > 0) {
				for (CategoryBrandAssociate categoryBrandAssociate : categoryBrandAssociateList) {
					List<CategoryBrandAssociate> categoryBrandAssociates;
					if (categoryBrandAssociateMap.containsKey(categoryBrandAssociate.getSkuCategoryBrandId())) {
						categoryBrandAssociates = categoryBrandAssociateMap.get(categoryBrandAssociate.getSkuCategoryBrandId());
					} else {
						categoryBrandAssociates = new LinkedList<>();
					}
					categoryBrandAssociates.add(categoryBrandAssociate);
					categoryBrandAssociateMap.put(categoryBrandAssociate.getSkuCategoryBrandId(), categoryBrandAssociates);
				}
			}
			for (Integer categoryBrandId : categoryBrandIdList) {
				//skuItemId和CategoryBrandAssociate映射关系
				Map<Integer, CategoryBrandAssociate> categoryBrandAssoMap = new LinkedHashMap<>();
				List<CategoryBrandAssociate> categoryBrandAssociates = categoryBrandAssociateMap.get(categoryBrandId);
				if (categoryBrandAssociates != null) {
					for (CategoryBrandAssociate categoryBrandAssociate : categoryBrandAssociates) {
						categoryBrandAssoMap.put(categoryBrandAssociate.getSkuItemId(), categoryBrandAssociate);
					}
				}
				List<CategoryBrandAssoVO> categoryBrandAssoVOList = categoryBrandBusiUtil.getCategoryBrandAssoVOListByItem
						(skuItemNameMap, categoryBrandAssoMap);
				categoryBrandVOMap.put(categoryBrandId, categoryBrandAssoVOList);
			}
		}

		return categoryBrandVOMap;
	}
}
